package com.huawei.hotel.controller;

import com.huawei.hotel.vo.User;
import com.huawei.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author 18902
 * @date 2020/10/19 22:22
 * @desc 用户控制类
 */
@Controller
public class UserController {
    //依赖注入
    @Autowired(required=true)
    private UserService userService;

    /**
     * 用户登录
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(HttpServletRequest request, HttpServletResponse response, Model model) throws ServletException {
        String useracount = request.getParameter ("useracount");
        String password = request.getParameter ("password");
        String sub_login = request.getParameter ("sub_login");
        User user = userService.findUser (useracount, password);
        if (user != null) {
            if (sub_login.equals ("学生登录")) {
                request.getSession ().setAttribute ("USER_SESSION", user);
                return "student";
            } else if (sub_login.equals ("宿管登录")) {
                request.getSession ().setAttribute ("USER_SESSION", user);
                return "sugaun";

            } else if (sub_login.equals ("管理员登录")) {
                request.getSession ().setAttribute ("USER_SESSION", user);
                return "admin";
            }
        }
        model.addAttribute ("msg", "账户或密码错误，请重新输入！");
        return "login";
    }

    /**
     * 用户注册
     */
    // @RequestMapping(value = "/register",method = RequestMethod.POST)
    // @ResponseBody
    // public String register(HttpServletRequest request, Model model){
    //     String useracount = request.getParameter("useracount");
    //     System.out.println(useracount);
    //     User user = new User();
    //     user.setUseracount(useracount);
    //     int num=userService.register (user);
    //       if(num>0){
    //         model.addAttribute ("msg","注册成功！");
    //         return "ok";
    //       }else {
    //         model.addAttribute ("msg","注册失败！请重新注册！");
    //         return "failed";
    //       }
    // }

    @RequestMapping("/register")
    // @ResponseBody
    public String register(User user){
        System.out.println (user.toString ());
        int rows=userService.register (user);
        if(rows>0){
            System.out.println (user.toString ());
            return "login";
        }else {
            return "register";
        }
    }





    /**
     * 退出登录（退出之后返回首页
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate ();
        //重定向到首页的跳转方法
        return "login";
    }

    /**
     * 用户更新
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(User user){
        int rows=userService.updateUser (user);
        if(rows > 0){
            return "OK";
        }else{
            return "FAIL";
        }
    }
    /**
     * 通过id获取客户信息
     */
    @RequestMapping("/getUserById")
    @ResponseBody
    public User getCustomerById(Integer id) {
        User user = userService.getCustomerById(id);
        return user;
    }
}
